package com.cjjd.c.feature.home.chekin.pay;

import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.base.MyBaseAdapter;
import com.cjjd.c.bean.AddressListBean;
import com.cjjd.c.bean.PayItemBean;
import com.cjjd.c.bean.ReceiptBean;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/2.
 */

public class PayModeAdapter extends MyBaseAdapter {

    public static int TYPE_FROM_PAY = 0X00000001;
    // 来自退房时候， 需要发票 的页面
    public static int TYPE_FROM_RECEIPT_DEAL = 0X00000002;

    public static int TYPE_FROM_BILL = 0X00000003;
    // 来自退房时候，需要发票地址的页面
    public static int TYPE_FROM_RECEIPT_ADDRESS = 0X00000004;
    private int TYPE;

    public PayModeAdapter(FragmentActivity mContext) {
        super(mContext);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_pay_mode, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        if (TYPE == TYPE_FROM_PAY) {
            PayItemBean itemData = (PayItemBean) getItem(position);
            if (itemData.isSelected) {
                viewHolder.checkbox.setChecked(true);
            } else {
                viewHolder.checkbox.setChecked(false);
            }
            viewHolder.tvPayMode.setText(itemData.payName);
            if (position == 0) {
                // 微信支付
                viewHolder.ivPayLogo.setImageResource(R.drawable.icon_pay_wx);
            } else if (position == 1) {
                //支付宝支付
                viewHolder.ivPayLogo.setImageResource(R.drawable.icon_pay_zhifubao);
            }
        }

        if (TYPE == TYPE_FROM_RECEIPT_DEAL || TYPE == TYPE_FROM_BILL) {
            ReceiptBean itemData = (ReceiptBean) getItem(position);
            if (itemData.isSelected) {
                viewHolder.checkbox.setChecked(true);
            } else {
                viewHolder.checkbox.setChecked(false);
            }
            if ("1".equals(itemData.invoiceType)){
                viewHolder.ivPayLogo.setImageResource(R.drawable.icon_bill_gr);
                viewHolder.tvHint.setText("个人");


            }else if ("2".equals(itemData.invoiceType)){
                viewHolder.ivPayLogo.setImageResource(R.drawable.icon_bill_company);
                viewHolder.tvHint.setText(itemData.dutyParagraph);
            }
            viewHolder.tvPayMode.setText(itemData.company);
        }

        if (TYPE == TYPE_FROM_RECEIPT_ADDRESS){
            AddressListBean itemData = (AddressListBean) getItem(position);
            if (itemData.isSelected) {
                viewHolder.checkbox.setChecked(true);
            } else {
                viewHolder.checkbox.setChecked(false);
            }

            viewHolder.ivPayLogo.setVisibility(View.GONE);
            viewHolder.layout.setBackgroundColor(mContext.getResources().getColor(R.color.global_background));
            viewHolder.tvPayMode.setText(itemData.consignee);
            viewHolder.tvHint.setText(itemData.address);
        }


        return convertView;
    }


    /**
     * @param from TYPE_FROM_BILL, TYPE_FROM_PAY
     */
    public void setFrom(int from) {
        this.TYPE = from;
    }

    static class ViewHolder {
        @Bind(R.id.iv_pay_logo)
        ImageView ivPayLogo;
        @Bind(R.id.tv_pay_mode)
        TextView tvPayMode;
        @Bind(R.id.tv_hint)
        TextView tvHint;
        @Bind(R.id.checkbox)
        CheckBox checkbox;
        @Bind(R.id.layout)
        RelativeLayout layout;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
